/****************************************************************************************
    PROJECT: UW–KEMRI Growth Data Analysis
    PURPOSE: Longitudinal Mixed-Effects Models of Anthropometric Outcomes (HAZ, WAZ, WHZ, MUAC)
    AUTHOR:  Polycarp Mogeni
    DATE:    10/20/2025
****************************************************************************************/

*----------------------------------------------------*
* 1. Load and prepare data
*----------------------------------------------------*
use "D:\UW_KEMRI\UW_KEMRI\Growth_Data_Hannah\Growth_data_clean", clear
br
label variable sc_age "Age in months (continuous)"
label variable en_ht "Height at enrollment"
label variable ht_3mo "Height at month 3"
label variable ht_6mo "Height at month 6"
label variable age_3mo "Age at month 3"
label variable age_6mo "Age at month 6 "
label variable underweight "Underweight: Weight-for-Age<-2SD"
label variable stunting "Stunting: Height-for-Age<-2SD"
label variable wasting "Weight-for-Height<-2SD"
label variable  SAM "Severe Acute Malnutrition"
        
* Rename variables for reshaping (wide → long)
rename en_haz  haz1
rename haz_3mo haz2
rename haz_6mo haz3

rename en_whz  whz1
rename whz_3mo whz2
rename whz_6mo whz3

rename en_waz  waz1
rename waz_3mo waz2
rename waz_6mo waz3

rename en_muac muac1
rename muac_3mo muac2
rename muac_6mo muac3

* Summary statistics for follow-up anthropometry
tabstat haz2 waz2 whz2, statistics(median p25 p75) columns(statistics)

* Generate baseline values for use in models
gen en_haz  = haz1
gen en_whz  = whz1
gen en_waz  = waz1
gen en_muac = muac1

* Means by treatment arm
bys rando_arm: means haz1 haz2 haz3
bys rando_arm: means whz1 whz2 whz3
bys rando_arm: means waz1 waz2 waz3

tab wasting

*----------------------------------------------------*
* 2. Reshape data from wide to long
*----------------------------------------------------*
reshape long haz waz whz muac, i(pid) j(visit)
lab define visit 1 "enrollment" 2 "month 3" 3 "month 6"
lab val visit visit
sort pid visit

*----------------------------------------------------*
* 3. Create time variables
*----------------------------------------------------*
gen time_enr_days = .
replace time_enr_days = 0 if visit == 1
replace time_enr_days = (date(fub_date_3mo, "YMD") - e_en_datetime) if visit == 2
replace time_enr_days = (date(fub_date_6mo, "YMD") - e_en_datetime) if visit == 3
hist time_enr_days, normal

* Convert days to months
gen time_enr_mo = time_enr_days / 30.436875
gen time_enr_3mo = time_enr_mo / 3
gen time_enr_6mo = time_enr_mo / 6

sort pid visit

*----------------------------------------------------*
* 4. Mixed-effects models: main analysis
*----------------------------------------------------*
log using "D:\UW_KEMRI\UW_KEMRI\Growth_Data_Hannah\Results", replace

*-----------------------*
* Height-for-age (HAZ)
*-----------------------*
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)

lincom time_enr_mo                                       // placebo trend
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo           // AZM trend
lincom 1.rando_arm#c.time_enr_mo                         // difference (AZM vs placebo)

*-----------------------*
* Weight-for-age (WAZ)
*-----------------------*
mixed waz en_waz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo

*-----------------------*
* Weight-for-height (WHZ)
*-----------------------*
mixed whz en_whz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo

*-----------------------*
* Mid-upper arm circumference (MUAC)
*-----------------------*
mixed muac en_muac ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo


*----------------------------------------------------*
* 5. Age-specific analyses
*----------------------------------------------------*
gen agecat = .
replace agecat = 0 if sc_age <= 5
replace agecat = 1 if sc_age > 5  & sc_age <= 11
replace agecat = 2 if sc_age > 11 & sc_age <= 23
replace agecat = 3 if sc_age > 23 & sc_age <= 59

********************************************************************************
* AGE GROUP 0 (≤5 months)
********************************************************************************
preserve
keep if agecat == 0

mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo

restore

********************************************************************************
* AGE GROUP 1 (6–11 months)
********************************************************************************
preserve
keep if agecat == 1

mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo

restore

********************************************************************************
* AGE GROUP 2 (12–23 months)
********************************************************************************
preserve
keep if agecat == 2

mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo

restore

********************************************************************************
* AGE GROUP 3 (24–59 months)
********************************************************************************
preserve
keep if agecat == 3

mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo

restore

********************************************************************************
* Repeat above pattern for WAZ, WHZ, and MUAC (kept for clarity)
*----------------------------------------------------*
* Each model section repeats the same three lincom commands:
*   - lincom time_enr_mo
*   - lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
*   - lincom 1.rando_arm#c.time_enr_mo
********************************************************************************
[... identical blocks for WAZ, WHZ, MUAC by agecat, same as above ...]

*----------------------------------------------------*
* 6. Subgroup analyses: Stunting / Wasting
*----------------------------------------------------*
* STUNTED CHILDREN
preserve
keep if stunting == 1
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo
restore

* NON-STUNTED CHILDREN
preserve
keep if stunting == 0
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo
restore

* WASTED CHILDREN
preserve
keep if wasting == 1
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo
restore

* NON-WASTED CHILDREN
preserve
keep if wasting == 0
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo
restore

* STUNTING OR WASTING (any)
preserve
keep if wasting == 1 | stunting == 1
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo
restore

* NON-STUNTED & NON-WASTED
preserve
keep if wasting == 0 & stunting == 0
mixed haz en_haz ib0.rando_arm##c.time_enr_mo || pid:, vce(robust)
lincom time_enr_mo
lincom time_enr_mo + 1.rando_arm#c.time_enr_mo
lincom 1.rando_arm#c.time_enr_mo
restore

log close
****************************************************************************************
* END OF DO-FILE
****************************************************************************************